<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021.3.19
 * Time: 15:41
 */

interface Observer {
    public function addObserver($type,$observer);

    public function observer($type);
}



class Order
{
    protected $observers = [];

    public function addObserver($type,$observer)
    {
        $this->observers[$type][] = $observer;
    }

    public function observer($type)
    {
        if (isset($this->observers[$type])) {
            foreach ($this->observers[$type] as $ob) {
                $a = new $ob;
                $a::update($this);
            }
        }
    }

    public function create()
    {
        echo "购买成功\r\n";
        $this->observer('buy');
    }
}

class orderEmail
{
    public static function update($order)
    {
        echo '发送购买成功一个邮件';
    }
}

class orderStatus
{
    public static function update($order)
    {
        echo '改变订单状态';
    }
}

$ob = new Order();
$ob->addObserver('buy',orderEmail::class);
$ob->addObserver('buy',orderStatus::class);
$ob->create();